package com.laxcen.controller2;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailRequest;
import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailResponse;
import com.aliyuncs.linkvisual.model.v20180120.QueryLiveStreamingRequest;
import com.aliyuncs.linkvisual.model.v20180120.QueryLiveStreamingResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import io.swagger.annotations.Api;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "视频")
@RestController
public class DeviceVideoController {

    DefaultAcsClient client = null;
    IClientProfile profile = null;

    public DeviceVideoController() {
        String accessKey = "LTAI5tQqhoe1XSrkoCWkDDQ9";
        String accessSecret = "MXycRe3SmNlDPKuLNt1cfxhFyjk5Cf";
        String regionId = "cn-shanghai";
        DefaultProfile.addEndpoint(regionId, "linkvisual", "linkvisual.cn-shanghai.aliyuncs.com");

        profile = DefaultProfile.getProfile(regionId, accessKey, accessSecret);// 初始化客户端
        client = new DefaultAcsClient(profile);
    }

    @GetMapping("/QueryRecordVideoURL")
    public VideoResponse QueryRecordVideoURL(@RequestParam(value = "IotId", defaultValue = "") String IotId,
            @RequestParam(value = "ProductKey", defaultValue = "a1VQrWIUFTW") String ProductKey,
            @RequestParam(value = "DeviceName", defaultValue = "Oufu003") String DeviceName,
            @RequestParam(value = "CameraNumber", defaultValue = "1") Integer CameraNumber,
            @RequestParam(value = "BeginTime", defaultValue = "0") Integer BeginTime,
            @RequestParam(value = "EndTime", defaultValue = "10") Integer EndTime) {

        VideoResponse videoResponse = new VideoResponse();
        try {

            QueryLiveStreamingRequest queryLiveStreamingRequest = new QueryLiveStreamingRequest();

            if (IotId == null || IotId.length() <= 0) {
                IotId = getIotId(ProductKey, DeviceName);
            }
            queryLiveStreamingRequest.setIotId(IotId);
            preInformCameraNumber(IotId, CameraNumber, 0, BeginTime, EndTime);

            // getQueryRecordTimeList(IotId);
            queryLiveStreamingRequest.setScheme("flv");

            QueryLiveStreamingResponse r = client.getAcsResponse(queryLiveStreamingRequest, profile);
            if (r.getSuccess()) {
                videoResponse.isSucess = true;
                videoResponse.videoUrl = r.getData().getPath();
            } else {
                throw new Exception(r.getErrorMessage());
            }
        } catch (Exception exp) {
            videoResponse.errorMsg = exp.getMessage();
            videoResponse.isSucess = false;

        }
        return videoResponse;
    }

    private String getIotId(String ProductKey, String DeviceName) throws Exception {
        QueryDeviceDetailRequest queryDeviceDetailRequest = new QueryDeviceDetailRequest();
        // queryDeviceDetailRequest.setIotId("a12bM7wcGVy");
        queryDeviceDetailRequest.setProductKey(ProductKey);
        queryDeviceDetailRequest.setDeviceName(DeviceName);
        QueryDeviceDetailResponse r = client.getAcsResponse(queryDeviceDetailRequest, profile);
        if (!r.getSuccess()) {
            throw new Exception(r.getErrorMessage());
        } else {
            return r.getData().getIotId();
        }

    }

    // 通知设备马上要使用几号摄像头，并指定是否直播，如果是录播的话，需要指定开始和结束时间
    private String preInformCameraNumber(String IotId, Integer CameraNumber, Integer IsLive, Integer BeginTime,
            Integer EndTime) throws Exception {

        JSONObject jObject = new JSONObject();
        jObject.put("CameraNumber", CameraNumber);
        jObject.put("IsLive", IsLive);
        jObject.put("BeginTime", BeginTime);
        jObject.put("EndTime", EndTime);

        InvokeThingServiceRequest invokeThingServiceRequest = new InvokeThingServiceRequest();
        invokeThingServiceRequest.setIotId(IotId);

        invokeThingServiceRequest.setIdentifier("SetCameraNumber");
        invokeThingServiceRequest.setArgs(jObject.toString());
        // invokeThingServiceRequest.setArgs("{\"CameraNumber\":" + CameraNumber + "}");
        InvokeThingServiceResponse r = client.getAcsResponse(invokeThingServiceRequest, profile);

        if (!r.getSuccess()) {
            throw new Exception(r.getErrorMessage());
        } else {
            return "ok";
        }

    }

    @GetMapping("/QueryLiveVideoURL")
    public VideoResponse QueryLiveVideoURL(@RequestParam(value = "IotId", defaultValue = "") String IotId,
            @RequestParam(value = "CameraNumber", defaultValue = "1") Integer CameraNumber,
            @RequestParam(value = "ProductKey", defaultValue = "a1VQrWIUFTW") String ProductKey,
            @RequestParam(value = "DeviceName", defaultValue = "Oufu003") String DeviceName) throws Exception {
        VideoResponse videoResponse = new VideoResponse();
        try {
            QueryLiveStreamingRequest queryLiveStreamingRequest = new QueryLiveStreamingRequest();
            if (IotId == null || IotId.length() <= 0) {
                IotId = getIotId(ProductKey, DeviceName);
            }

            queryLiveStreamingRequest.setIotId(IotId);
            preInformCameraNumber(IotId, CameraNumber, 1, 0, 0);

            queryLiveStreamingRequest.setScheme("flv");
            QueryLiveStreamingResponse r = client.getAcsResponse(queryLiveStreamingRequest, profile);

            if (r.getSuccess()) {
                videoResponse.isSucess = true;
                videoResponse.videoUrl = r.getData().getPath();
            } else {
                throw new Exception(r.getErrorMessage());
            }
        } catch (

        Exception exp) {
            videoResponse.errorMsg = exp.getMessage();
            videoResponse.isSucess = false;

        }
        return videoResponse;
        // return String.format("Hello %s!", name);
    }

    class VideoResponse {
        public String videoUrl;
        public Boolean isSucess;
        public String errorMsg;
    }
}
